/**
 * Created by zhangyi on 2017/11/10.
 */
mainApp.factory("departFactory",function() {
    var service={};

    var object="";
    service.setDepart=function(data){
        object=data;
    }
    service.getDepart=function(){
        return object;
    }

    //获取所有列表service
    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list="";
        RequestUtils.postJson('../depart/get',obj).done(function(resp){
            if(resp.resultCode==0){
                console.log(resp.data);
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });
        return list;
    };

    //新增部门的service
    service.addDepartData=function(departObj){
        console.log(departObj)
        if(departObj.name!=null&&departObj.name!="" && departObj.manager!=null&&departObj.manager!=""){

        RequestUtils.postForm("../depart/add",departObj)
            .done(function(resp){
                console.log(resp);
                if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint('add',"成功");
                }else{
                    CommonUtils.buildFailHint('add',"失败");
                }
            });
        }
    };

    //据id查询部门service
    service.getDepartById=function(id){
        var list="";
        RequestUtils.getJson("../depart/getById/"+id)
            .done(function(resp){
                console.log(resp);
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint("查询数据","成功");
                }else{
                    CommonUtils.buildFailHint("查询数据","失败");
                }
            });
        return list;
    };

    //修改数据service
    service.update=function(departObj){
        if(departObj.name!=null&&departObj.name!="" && departObj.manager!=null&&departObj.manager!="") {

            RequestUtils.postForm("../depart/update", departObj)
                .done(function (resp) {
                    if (resp.resultCode == 0) {
                        CommonUtils.buildSuccessHint("update", "成功");
                    } else {
                        CommonUtils.buildFailHint("update", "失败");
                    }
                });
        }
    };

    //删除数据serviec
    service.delete=function(id){
        RequestUtils.getJson("../depart/delete/"+id)
            .done(function(resp){
                if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint('delete',"成功");
                    location.reload(true);
                }else{
                    CommonUtils.buildFailHint('delete',"失败");
                }
            });
    };

    //查询所有service
    service.selectAll=function(){
        var list="";
        RequestUtils.getJson('../depart/getAll').done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("查询数据","成功");
            }else{
                CommonUtils.buildFailHint('查询数据', '失败');
            }
        });
        return list;
    };

    return service;
});

mainApp.controller("departListController",function($scope,$http,$state,departFactory,empFactory,overallSituationService) {
    overallSituationService.loginIntercept();

    //首页
    $scope.sumweekList=departFactory.getListData(1);
    $scope.list=$scope.sumweekList.items;

    //查询工号，跳转修改页面
    $scope.selectDepart=function(id){
        //添加标签
        var tab={url:"depart-update",text:"部门详情",id:"depart-update-tab",closeable:1};
        $scope.$emit('addTab', tab);

        var depart=departFactory.getDepartById(id);
        departFactory.setDepart(depart);
        $state.go('depart-update');
    }

    //删除数据
    $scope.delete=function(id){
        departFactory.delete(id);
    };

    $scope.addTab=function(){
        var tab={url:"depart-add",text:"部门新增",id:"depart-add-tab",closeable:1};
        $scope.$emit('addTab', tab);
    };

    //翻页
    $scope.NextListData=function(pageIndex){
        $scope.sumweekList=departFactory.getListData(pageIndex);
        $scope.list=$scope.sumweekList.items;
    }

    //在cookie中获取工号
    var jobnum=CommonUtils.getCookie("login_usergonghao").jobnum;
    console.log(jobnum);
    empFactory.getAllByEmpIdOrName(jobnum);
});

mainApp.controller("departAddController",function($scope,$http,departFactory,empFactory) {
    //新增
    $scope.departManager;
    $scope.manager;
    $scope.addDepart=function(departName,managerObj){
        console.log(managerObj);
        var depart={
            "name":departName,
            "manager_name":managerObj.name,
            "phone":managerObj.phone,
            "email":managerObj.email,
            "manager":managerObj.id
        };
        console.log(depart);
        departFactory.addDepartData(depart);

        //$scope.updateDepart="";
        $scope.departManager=empList;
    };

    //在员工处查询的所有员工信息
    var empList=empFactory.selectAll();
    $scope.departManager=empList;
});

mainApp.controller("departUpdateController",function($scope,$http,departFactory,empFactory) {
    //据id查询时得到的部门和主管信息
    var departObj=departFactory.getDepart();
    //联查的个人信息，显示原有的信息
    $scope.updateManager={
        "id":departObj.manager,
        "name":departObj.manager_name,
        "phone":departObj.phone,
        "email":departObj.email
    }
    //一条部门信息
    $scope.updateDepart={
        "id":departObj.id,
        "name":departObj.name,
        "manager":departObj.manager,
        "manager_name":departObj.manager_name,
        "phone":departObj.phone,
        "email":departObj.email
    };

    //在员工处查询的所有员工信息
    var empList=empFactory.selectAll();
    $scope.departManager=empList;

    //修改数据
    $scope.Update=function(managerObj,departObj){
        var depart={
            "id":departObj.id,
            "name":departObj.name,
            "manager_name":managerObj.name,
            "phone":managerObj.phone,
            "email":managerObj.email,
            "manager":managerObj.id
        };
        console.log(depart);
        departFactory.update(depart);

        //$scope.updateDepart=""
        $scope.departManager=empList;
    };
});